package com.groupbuying.storeinternal.utils;

import com.groupbuying.storeinternal.utils.apiresult.ApiResultCode;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @anthor wenguoqiang
 * @date: 2019/01/16 12:09
 * @company 蒙泰互联网公司
 * @des
 **/
public class ReturnUtils {

    public static Map getExceptionResult(Logger log,Exception e){

        String errId = "ERRID" + UUID.randomUUID();
        log.error(errId , e);
        String errMsg = "[" + errId + "][" + e.getMessage() + "]";
        return  createReturnResult(ApiResultCode.OPRFAILURE.code(),errMsg,false,"");
    }
    public static String appendResultStr(Logger log,String responseStr,String methodName){

        //追加调用方法名，以便android logcat看日志调试
        //
        String methodStr = ",\"client_method_name\":"+"\""+methodName+"\"";
        StringBuffer logStr = new StringBuffer();

        if(responseStr.length() > 0) {
            StringBuffer buf = new StringBuffer(responseStr);
            buf.insert(responseStr.length() - 1, methodStr);
            logStr.append("\nAPP POST 返回信息 : " + buf.toString());
        }else{
            logStr.append("\nAPP POST 返回信息 : " + responseStr);
        }

        log.debug(logStr.toString());
        return responseStr;
    }
    public static Map paramIsNull(){

        return   createReturnResult(ApiResultCode.PARAMNULL.code(),ApiResultCode.PARAMNULL.message(),false,"");
    }

    public static Map createReturnResult(Integer Code,String message, Boolean isSuccess, String result){
        Map returnMap = new HashMap();
        returnMap.put("resultCode", Code);
        returnMap.put("resultMessage", message);
        returnMap.put("success", isSuccess );
        returnMap.put("result", result);
        return returnMap;
    }

}
